/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MpmProjectChangeManagerService
* 项目经理变更
*
* 修改纪录
* 2021-11-19 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2021-11-19
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mpm.pojo.MpmProjectReviewEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectChangeManagerService;
import com.topscomm.mpm.service.impl.auto.MpmProjectChangeManagerServiceAuto;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.mpm.pojo.MpmProjectChangeManagerEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service(value = "mpmProjectChangeManagerService")
@Scope(value = "singleton")
public class MpmProjectChangeManagerService extends MpmProjectChangeManagerServiceAuto implements IMpmProjectChangeManagerService {
	@Autowired
	private MpmProjectChangeManagerInfoService mpmProjectChangeManagerInfoService;
	@Autowired
	private MpmProjectService mpmProjectService;
	/**
	 * @author: houyuhang
	 * @date: 2021-11-19
	 * @param entity
	 * @throws ServiceException
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(MpmProjectChangeManagerEntity.FieldDocno, docNo);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-01-25
	 * @description: 项目交接详情表保存
	 * @modify:
	 */
	@Override
	protected void updateAfter(Map<String, Object> map) throws ServiceException {
		super.updateAfter(map);
		// 删除原内容，增加新内容
		long parentid = ConvertUtil.convertToLong(map.get("id"));
		List<Map<String, Object>> infoList = (List<Map<String, Object>>) map.get("managerinfo");
		mpmProjectChangeManagerInfoService.insertChangeManagerInfo(parentid, infoList);
	}
	/**
	 * @author: houyuhang
	 * @date: 2023-06-01
	 * @description: 判断是否为本省人员
	 * @modify:
	 */
	@Transactional
	public boolean checkSameProvinceStaff(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" cbouser.code = '{0}' ", paraMap.get("projectmanagercode"));
		List<Map<String, Object>> userDeptList = mpmProjectService.queryMapByWhere(sql.toString(), "queryCboUserDept");
		sql.clear();
		sql.appendSingle("select deptcode from mmsdeptextrainfo where cfocode = '{0}' ", paraMap.get("usercode"));
		List<Map<String, Object>> cfoDeptList = this.selectExecute(sql.toString());
		if (!CollectionUtil.isEmpty(userDeptList) && !CollectionUtil.isEmpty(cfoDeptList)) {
			String userdeptcode = ConvertUtil.convertToString(userDeptList.get(0).get("projectdeptcode"));
			String cfodeptcode = ConvertUtil.convertToString(cfoDeptList.get(0).get("deptcode"));
			return cfodeptcode.contains(userdeptcode);
		}
		return true;
	}
	/**
	 * @author:houyuhang
	 *      * @date: 2022-08-01
	 *      * @throws ServiceException
	 *      * @description: 删除复盘时同步删除中标信息
	 *      * @modify:
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	protected void deleteBefore(String whereSql) {
		super.deleteBefore(whereSql);
		// 找到删除manager内容
		List<MpmProjectChangeManagerEntity> managerEntities = this.queryByWhere(whereSql);
		if (CollectionUtil.isEmpty(managerEntities)) {
			return;
		}
		// 删除子表信息
		StringBufferProxy delsql = new StringBufferProxy();
		delsql.appendSingle(" parentid in ({0}) ", managerEntities.stream().map(item -> ConvertUtil.convertToString(item.getId())).collect(Collectors.joining(",")));
		mpmProjectChangeManagerInfoService.deleteByWhere(delsql.toString());
	}
}
